//
//  ShareResouceDetailViewController.m
//  ShangHaiHuDongApp
//
//  Created by 刘应平 on 2017/6/22.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "ShareResouceDetailViewController.h"
#import "ShareOrderActServiceViewController.h"
#import "PartyServiceCenterModel.h"
#import "ShareResouceDetailTableViewCell.h"
#import "ShareResouceDetailModel.h"
#import "ShareResouceDetailFooterView.h"
@interface ShareResouceDetailViewController ()<UITableViewDelegate,UITableViewDataSource,ShareResouceDetailFooterDelegate>
/**  tableview  */
@property (strong, nonatomic) UITableView  *tableView;
/**  提交  */
@property (strong, nonatomic) UIButton  *sendButton;
/**  数据  */
@property (strong, nonatomic) NSArray  *dataSouce;


@end

@implementation ShareResouceDetailViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
//    [self netWorking_Detail];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.tableView];
    if (self.ID) {
        [self netWorking_Detail];
    }else{
        self.ID = self.model.ID;
        self.type = self.model.TYPE;
        [self getDataSource];
    }
}

- (void)action_leftButton:(UIButton *)sender{
    if (self.isModel) {
        [LPCostomPressentAnimationSimilarityPushAnimation backLastVc];
        [self dismissViewControllerAnimated:NO completion:nil];
    }else{
        [self.navigationController popViewControllerAnimated:YES];
    }
}
//网络请求资源详情
- (void)netWorking_Detail{
  
     [YJProgressHUD showMessage:@"加载中..." duration:10 view:self.view];
    NSDictionary *dict = @{@"ID":self.ID,@"TYPE":self.type};
    __weak typeof(self) weakSelf = self;
    [NetWorkingManager postWithUrlString:[NetWorkingURL getSingleResource] Parameter:dict Success:^(id object) {
        [YJProgressHUD hide];
        NSString *string = [[object objectForKey:@"RESULT"] objectForKey:@"STATE"];
        if ([string isEqualToString:@"20000"]) {
            weakSelf.model = [ShareOpenListModel yy_modelWithJSON:[[object objectForKey:@"RESULT"] objectForKey:@"RESOURCEINFO"]];
            weakSelf.ID = weakSelf.model.ID;
            weakSelf.type = weakSelf.model.ID;
            [weakSelf getDataSource];
            [weakSelf.tableView reloadData];
        }
    } Fail:^(id object) {
        [YJProgressHUD hide];
        
        [LYEmptyManager showLoadFailWithView:weakSelf.tableView managerBlock:^{
            [weakSelf netWorking_Detail];
        }];

    }];
}


#pragma mark - 数据
- (void)getDataSource{
    if (!self.model || !self.model.ID) {//数据库没有改需求信息
        __weak typeof(self) weakSelf = self;
        UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"该资源已被删除" preferredStyle:UIAlertControllerStyleAlert];
        [alertController addAction:[UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            if (weakSelf.block) {
                weakSelf.block();
            }
            [weakSelf action_leftButton:weakSelf.leftButton];
        }]];
        [self presentViewController:alertController animated:YES completion:nil];
        return;
    }

    self.titlelabel.text = self.model.NAME;
    NSString *start = [self.model.STARTTIME substringToIndex:5];
    NSString *end = [self.model.ENDTIME substringToIndex:5];
    NSString *time = [NSString stringWithFormat:@"%@-%@",start,end];
    
    NSString *number = @"一次性预约人数";
    NSString *name = @"资源名称";
    NSString *address = @"场馆地址";
    if (self.isJFDH) {
        number = @"库存数量";
        name = @"物品名称";
        address = @"兑换地址";
    }
    NSArray *array = @[
                       @{@"title": name, @"content": self.model.NAME},
                       @{@"title": @"提供方", @"content": self.model.RESOURCE_PRINCIPAL},
                       @{@"title": @"开放时间", @"content": time},
                       @{@"title": number, @"content": self.model.PEOPLE_NUM},
                       @{@"title": address, @"content": [NSString string:self.model.ADDRESS isNilStringReturnDeftString:@"暂无"]}];
    
    self.dataSouce = [NSArray yy_modelArrayWithClass:[ShareResouceDetailModel class] json:array];
    [self layoutUI];
}


#pragma mark - 界面
- (void)layoutUI{
    
    CGFloat imageHeight = 0.0;
    CGFloat width = ([UIScreen zj_screenWith] - 70 * [UIScreen zj_screenWScale]) / 4;
    NSInteger zheng = self.model.IMGJSON.count / 4;
    NSInteger yu = self.model.IMGJSON.count % 4;
    if (yu > 0) {
        zheng += 1;
    }
    imageHeight = zheng * width + 10 * [UIScreen zj_screenHScale] * (zheng - 1);
    CGFloat height = [CalculateLabelHeight calculateLabeHeightWithString:self.model.DESCRIPTION font:15 * [UIScreen zj_textFontScale] width:[UIScreen zj_screenWith] - 40 * [UIScreen zj_screenHScale] space:4 * [UIScreen zj_screenHScale]];
    CGFloat showHeight = 0.0;
    if (self.isNoShow) {
        showHeight = 70 * [UIScreen zj_screenHScale];
    }else{
        showHeight = 180 * [UIScreen zj_screenHScale];
    }
    
    
    ShareResouceDetailFooterView *fotter = [[ShareResouceDetailFooterView alloc] initWithFrame:CGRectMake(0, 0, self.view.width,imageHeight + height + showHeight)];
    fotter.delegate = self;
    fotter.isJFDH = self.isJFDH;
    fotter.height = height;
    fotter.TORANGE = self.model.TORANG;
    fotter.contentString = self.model.DESCRIPTION;
    fotter.images = self.model.IMGJSON;
    fotter.isNoShow = self.isNoShow;
    self.tableView.tableFooterView = fotter;
}



#pragma mark - ShareResouceDetailFooterDelegate
- (void)ShareResouceDetailFootertap_click{
    ShareOrderActServiceViewController *vc = [ShareOrderActServiceViewController new];
    vc.isJFDH = self.isJFDH;
    vc.model = self.model;
    [self.navigationController pushViewController:vc animated:YES];

}



#pragma mark - UITableViewDataSource,UITableViewDelegate
/**  返回组个数  */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
/**  返回cell个数  */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSouce.count;
}
/**  返回cell高度  */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    ShareResouceDetailModel *model = self.dataSouce[indexPath.row];
    CGFloat height = 0.0;
    if ([model.cellHEIGHT floatValue] * [UIScreen zj_screenHScale] < model.allHeight) {
        height = model.allHeight;
    }else{
        height = [model.cellHEIGHT floatValue] * [UIScreen zj_screenHScale];
    }
    return height;
}
/**  cell初始化  */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *weizhi = [NSString stringWithFormat:@"%ld-%ld",(long)indexPath.section,(long)indexPath.row];
    ShareResouceDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:weizhi];
    if (!cell) {
        cell = [[ShareResouceDetailTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:weizhi];
    }
    cell.row = indexPath.row;
    cell.model = self.dataSouce[indexPath.row];
    return cell;
}

#pragma mark - 懒加载
/**  初始化tableView  */
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, LP_TopHeight, self.view.bounds.size.width, self.view.bounds.size.height - LP_TopHeight) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.separatorColor = [UIColor clearColor];
        _tableView.backgroundColor = [UIColor themeGrayColor];
    }
    return _tableView;
}

@end
